<template>
  <div class="breadcrumb_style">
    <el-breadcrumb class="app-breadcrumb" separator="/">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
          <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect">{{ item.meta.title }}</span>
          <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
    <span v-if="levelList.length>1 && this.levelList[1].name === 'Leads' " class="xs_style">
      <el-select
        v-model="clue"
        clearable
        style="width:240px"
        size="small"
        placeholder="请选择线索名称"
        @change="changeClue"
      >
        <el-option
          v-for="dict in List"
          :key="dict.id"
          :label="dict.name"
          :value="dict.id"
        />
      </el-select>
    </span>
  </div>
</template>
<script>
import { getClueList } from "@/api/crm/customer.js";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      levelList: null,
      clue: "",
      category: "",
      List: []
    };
  },
  computed: {
    ...mapGetters(["loadClueTime"])
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith("/redirect/")) {
        return;
      }
      this.getBreadcrumb();
    },
    "loadClueTime": function() {
      this.getClueList();
    }
  },
  created() {
    this.getBreadcrumb();
    this.getClueList();
  },
  methods: {
    changeClue() {
      this.$store.dispatch("settings/setClue", this.clue);
    },
    getClueList() {
      getClueList().then(res => {
        if (res.code === 200) {
          this.List = res.data.results;
        }
      });
    },
    getBreadcrumb() {
      // only show routes with meta.title
      const matched = this.$route.matched.filter(item => item.meta && item.meta.title);
      const first = matched[0];

      if (!this.isDashboard(first)) {
        // matched = [{ path: '/index', meta: { title: '首页' }}].concat(matched)
      }

      this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false);
    },
    isDashboard(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      return name.trim() === "首页";
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(path);
    }
  }
};
</script>

<style lang="scss" scoped>
.breadcrumb_style{
  .xs_style{
    position: absolute;
    top: 60px;
    right: 20px;
  }
}
.app-breadcrumb.el-breadcrumb {
  // display: inline-block;
  font-size: 14px;
  line-height: 54px;
  padding-left: 24px;
  background: #F4F7F9 ;

  .no-redirect.el-breadcrumb__separator{
    color: #333333 !important;
    cursor: text;
    font-weight: 600 !important;
  }
}
.el-breadcrumb__inner a{
  color: rgba(0, 0, 0, 0.4);
  // padding-left: 6px;
  // border-left: 2px solid #0052D9;
}
::v-deep .el-breadcrumb__separator{
color: #333333 !important;
font-weight: 600 !important;
}
</style>
